// SPDX-License-Identifier:	GPL-2.0+

#include <common.h>
#include <malloc.h>
#include <clk.h>
#include <led.h>
#include <pwm.h>
#include <pci.h>
#include <net.h>
#include <power/regulator.h>

#include <asm/io.h>
#include <asm/gpio.h>

#include <mach/ls2k.h>

#include "../common/platform.h"

DECLARE_GLOBAL_DATA_PTR;

#ifdef CONFIG_NAND_LS1X
static void ls2x_set_nand(void)
{
	u32 val = 0;
	void __iomem *mux_base = ioremap(LS2X_MUX_BASE, 1);

	/* 设置nand gpio复用 nand_sel=1 */
	val = readl(mux_base);
	val = val | (1<<9);
	writel(val, mux_base);
}
#endif

#ifndef NAND_LS2K_DMACHAN
#define NAND_LS2K_DMACHAN 0
#endif
#ifndef SDIO_LS2K_DMACHAN
#define SDIO_LS2K_DMACHAN 4
#endif

/* 设置设备使用的apb dma通道 */
static void ls2x_set_apbdma(void)
{
	u32 val;
	void __iomem *apb_dma_cfg = ioremap(LS2X_APB_DMA_CFG, 1);

	val = (NAND_LS2K_DMACHAN<<0) |
		(1<<3) | (2<<6) | (1<<9) | (2<<12) | (SDIO_LS2K_DMACHAN<<15) | (1<<18) | (3<<21);
	__raw_writel(val, apb_dma_cfg);
}

void wdt_stop(void)
{
	u32 val;
	void __iomem *gpio_base = ioremap(LS2X_GPIO_BASE, 1);
	//SM706T WDO out 0
	val = __raw_readl(gpio_base + 0x04);
	val = val & (~(1<<9));
	__raw_writel(val, gpio_base + 0x04);

	val = __raw_readl(gpio_base + 0x14);
	val = val & (~(1<<9));
	__raw_writel(val, gpio_base + 0x14);
}

int board_early_init_f(void)
{
	ls2x_set_apbdma();
#ifdef CONFIG_NAND_LS1X
	ls2x_set_nand();
#endif
	wdt_stop();

	return 0;
}

int ls2k_board_late_init(void)
{
	return 0;
}
